
/*
* 基础DEMO URL https://pc.d1-zhan.com/wx-upload/designImg/2019/09-16/1/small-1.jpg
* */
import { HOST } from "../constances/server";

import { urlFix } from './http'

function Get(url, params, cb, isLoad = false) {
  let cookie = wx.getStorageSync("cookie")//读取sessionid,当作cookie传入后台将PHPSESSID做session_id使用
  // if (isLoad === true) {
  //   wx.showLoading()  //顶部显示loading效果
  // }
  wx.getStorage({
    key: 'token',
    success(res) {
      let token = res.data
      if (token) {
        params = Object.assign(params, {token: token})
      }
      wx.request({
        url: urlFix(url),
        data: params,
        header: {
          contentType: 'content-type:application/json;charset=utf-8', //跨域请求
          'cookie': cookie,
        },
        success: (res) => {
          typeof cb == "function" && cb(res.data, "");
          // if (isLoad === true) {
          //   wx.hideLoading()
          // }
          if (!cookie) wx.setStorageSync('cookie', res.header['Set-Cookie']); //保存Cookie到Storage
          checkCode(res.data.code);//判断状态码进行操作
        },
        fail: (err) => {
          typeof cb == "function" && cb(null, err.errMsg);
          console.log(err)
          // if (isLoad === true) {
          //   wx.hideLoading()
          // }
        }
      })
    },
    fail () {
      wx.request({
        url: urlFix(url),
        data: params,
        header: {
          contentType: 'content-type:application/json;charset=utf-8', //跨域请求
          'cookie': cookie,
        },
        success: (res) => {
          typeof cb == "function" && cb(res.data, "");
          // if (isLoad === true) {
          //   wx.hideLoading()
          // }
          if (!cookie) wx.setStorageSync('cookie', res.header['Set-Cookie']); //保存Cookie到Storage
          checkCode(res.data.code);//判断状态码进行操作
        },
        fail: (err) => {
          typeof cb == "function" && cb(null, err.errMsg);
          console.log(err)
          // if (isLoad === true) {
          //   wx.hideLoading()
          // }
        }
      })
    }
  })
};

function Post(url, params, cb, isLoad = true) {
  let cookie = wx.getStorageSync("cookie")//读取sessionid,当作cookie传入后台将PHPSESSID做session_id使用
  // if (isLoad === true) {
  //   wx.showLoading()  //顶部显示loading效果
  // }
  wx.getStorage({
    key: 'token',
    success(res) {
      let token = res.data
      if (token) {
        params = Object.assign(params, {token: token})
      }
      wx.request({
        method: 'POST',
        url: urlFix(url),
        data: params,
        header: {
          "Content-Type": "application/x-www-form-urlencoded",//跨域请求
          'cookie': cookie,
        },
        success: (res) => {
          typeof cb == "function" && cb(res.data, "");
          // if (isLoad === true) {
          //   wx.hideLoading()
          // }
          if (!cookie) wx.setStorageSync('cookie', res.header['Set-Cookie']); //保存Cookie到Storage
          checkCode(res.data.code);//判断状态码进行操作
        },
        fail: (err) => {
          typeof cb == "function" && cb(null, err.errMsg);
          console.log(err);
          // if (isLoad === true) {
          //   wx.hideLoading()
          // }
        }
      });
    },
    fail () {
      wx.request({
        method: 'POST',
        url: urlFix(url),
        data: params,
        header: {
          contentType: 'content-type:application/json;charset=utf-8', //跨域请求
          'cookie': cookie,
        },
        success: (res) => {
          typeof cb == "function" && cb(res.data, "");
          // if (isLoad === true) {
          //   wx.hideLoading()
          // }
          if (!cookie) wx.setStorageSync('cookie', res.header['Set-Cookie']); //保存Cookie到Storage
          checkCode(res.data.code);//判断状态码进行操作
        },
        fail: (err) => {
          typeof cb == "function" && cb(null, err.errMsg);
          console.log(err)
          // if (isLoad === true) {
          //   wx.hideLoading()
          // }
        }
      })
    }
  })

};

function loginFunc() {
  wx.getStorage({
    key: 'userInfo',
    success(res) {
      console.log(res)
      let user = res.data
      return user
    },
    fail() {

    }
  })
}

//判断固定的状态码进行固定的操作
function checkCode(code){
  if(code === 401){
    //用户未授权
    wx.showToast({
      duration: 2000,
      icon: 'none',
      title: '请先进行授权'
    })
    setTimeout(() => {
      wx.navigateTo({
        url: '/pages/empower/index',
      })
    }, 2000)
  } else if (code === 402){
    //用户未绑定手机
    wx.showToast({
      duration: 2000,
      icon: 'none',
      title: '请先绑定手机'
    })
    setTimeout(() => {
      wx.navigateTo({
        url: '/pages/binding/index',
      })
    }, 2000)
  }
}

//暴露接口
module.exports = {
  baseGet: Get,
  basePost: Post,
  checkLogin: loginFunc,
}
